package ru.arriah.servicedesk.web.client.action;

import java.io.IOException;
import java.util.Collection;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.IntWrapper;
import ru.arriah.servicedesk.bean.RequestBean;
import ru.arriah.servicedesk.bean.RequestStatusBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.InternalException;

public class DisplayRequestsListAction extends DumbAction {
   protected RequestManagerLocalHome requestManagerHome;

   public DisplayRequestsListAction() throws NamingException {
      super();

      requestManagerHome =
         Utils.getRequestManagerHomeInterface();
   }
   
   
   public DisplayRequestsListAction(RequestManagerLocalHome requestManagerHome) {
      super();
      this.requestManagerHome = 
         requestManagerHome;         
   }

   
   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
      try {         

        int page = idVal(request.getParameter("page"));
        int itemsPerPage = Integer.parseInt(config.getProperty("ITEMS_PER_PAGE"));
        IntWrapper records = new IntWrapper(0);       
        
        int clientId = Utils.getUserId(request);
        
        String orderBy = request.getParameter("orderBy");
        String order = request.getParameter("order");
        order = (order == null) ? "DESC" : order;
        orderBy = (orderBy == null) ? "N" : orderBy;        
        
        RequestManagerLocalObject requestManager = 
          requestManagerHome.create();
                        
        Collection<RequestBean> requestsList = 
          requestManager.selectRequestsByClient(
            clientId, 
            orderBy, 
            order,
            page,
            itemsPerPage,
            records
          );        
        
        this.pageNavigationToRequest(
          page,          
          records.getValue(), 
          "clientui?_action=listRequests&orderBy="+orderBy+"&order="+order+"&page=",
          request
        );
        
        Collection<RequestStatusBean> statusList = 
           requestManager.selectRequestStatuses();

        requestManager.remove();        
        
        request.setAttribute("statusList", statusList);
        request.setAttribute("requestsList", requestsList);
        request.setAttribute("order", (order.equals("ASC")) ? "DESC" : "ASC");
        request.setAttribute("action", "listRequests");
        
        return actionMapping.getSuccessTarget();
      } catch (Exception e) {
         handleException(e, "execute");
         throw new InternalException("ErrorMessage.internal");
      }
   }
}